package com.trkj.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.trkj.pojo.Emp;
import com.trkj.pojo.Quit;
import com.trkj.service.QuitService;
import com.trkj.service.WorkFlowService;
import com.trkj.vo.QuitVo;

@Controller
@SessionAttributes({"emp", "funcIds"})
public class QuitController {
	@Autowired
	QuitService quitService;
	@Autowired
	WorkFlowService workFlowService;
	
	/**
	 * 离职部署流程接口
	 * @author Lixd
	 */
	@RequestMapping("/bushu_quit")
	public void deploymentProcessDefinition_classpath() {
		workFlowService.deploymentProcessDefinition_classpath("人员离职流程",
				"diagrams/quit_actProcess.png","diagrams/quit_actProcess.bpmn");
	}
	
	/**
	 * 填写离职申请表单
	 * @return
	 */
	@RequestMapping("to_quit")
	public String quit() {
		return "to_quit";
	}
	
	/**
	 * 获取离职单信息
	 * @param model
	 * @return
	 */
	@RequestMapping("/get_quit")
	@ResponseBody
	public QuitVo getQuit(Model model) {
		Emp emp = (Emp) model.asMap().get("emp");
		Integer empid = emp.getEmpId();
		return quitService.quitVo(empid);
	}
	
	/**
	 * 离职申请暂存
	 * @param quit
	 * @param empId
	 * @param deptId
	 * @param positionId
	 * @return
	 */
	@RequestMapping("/save_quit")
	@ResponseBody
	public boolean save_quit(Quit quit,Integer empId,Integer deptId,Integer positionId) {
		Integer isSave = quitService.save_quit(quit, empId, deptId, positionId);
		if (isSave != 0) {
			return true;
		}
			return false;
	}
	/**
	 * 离职申请直接提交
	 * @author Lixd
	 * @param quit
	 * @param empId
	 * @param deptId
	 * @param positionId
	 * @return
	 */
	@RequestMapping("/submit_quit")
	@ResponseBody
	public boolean submit_quit(Quit quit,Integer empId,Integer deptId,Integer positionId,
			@RequestParam("pk")String processDefinitionKey,Model model) {
		Integer useid = getOnLineEmp(model);
		Integer processId = quitService.save_quit(quit, empId, deptId, positionId);
		if (processId != 0) {
			return workFlowService.startEmployProcess(processId, useid, processDefinitionKey);
		}
		return false;
	}
	/**
	 * 获取登录人的id
	 * @author Lixd
	 * @param model
	 * @return
	 */
	private Integer getOnLineEmp(Model model) {
		Emp emp = (Emp)model.asMap().get("emp");
		Integer useid = emp.getEmpId();
		return useid;
	}
}
